package com.itheima.web.manager;

import cn.hutool.core.date.DateUtil;
import com.itheima.service.db.AnalysisByDayService;
import com.itheima.vo.AnalysisSummaryVo;
import com.itheima.vo.UserFindVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AnalysisManager {

    @DubboReference
    private AnalysisByDayService analysisByDayService;

    // 统计展示
    public ResponseEntity findSummaryVo() {
        AnalysisSummaryVo analysisSummaryVo = analysisByDayService.findAnalysisSummaryVo();

        return ResponseEntity.ok(analysisSummaryVo);
    }

    //新增、活跃用户、次日留存率
    public ResponseEntity findView(Long startTime, Long endTime, Integer type) {
        //数据修改和封装

        String startDate = DateUtil.offsetDay(new Date(startTime), 0).toDateStr();
        String endDate = DateUtil.offsetDay(new Date(endTime), 0).toDateStr();

        String lastStartDate = DateUtil.offsetDay(new Date(startTime), -365).toDateStr();
        String lastEndDate = DateUtil.offsetDay(new Date(endTime), -365).toDateStr();

        List<UserFindVo> thisYear = analysisByDayService.findUsers(startDate, endDate, type);//今年的
        List<UserFindVo> lastYear = analysisByDayService.findUsers(lastStartDate, lastEndDate, type);//去年的

        //map集合进行封装
        Map<String,List> map=new HashMap<>();
        map.put("thisYear",thisYear);
        map.put("lastYear",lastYear);

        return ResponseEntity.ok(map);
    }
}
